package com.jy.adscm.operation.mscyb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.cg.bean.CgMaterial;
import com.jy.adscm.operation.cg.mapper.mysql.CgMaterialMapper;
import com.jy.adscm.operation.mscyb.bean.*;
import com.jy.adscm.operation.mscyb.mapper.mysql.*;
import com.jy.adscm.operation.mscyb.service.JqdbMachinepackingService;
import com.jy.adscm.operation.purchase.bean.Purchasebom;
import com.jy.adscm.operation.purchase.mapper.mysql.PurchasebomMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-06
 */
@Service
public class JqdbMachinepackingServiceImpl extends ServiceImpl<JqdbMachinepackingMapper, JqdbMachinepacking> implements JqdbMachinepackingService {
   @Autowired
    JyCodeMapper jyCodeMapper;
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    JqcpMachineproductMapper jqcpMachineproductMapper;
    @Autowired
    JqzzMachineMapper jqzzMachineMapper;
    @Autowired
    JqzzSettingMapper jqzzSettingMapper;
    @Autowired
    JqdbSettingsMapper jqdbSettingsMapper;
    @Autowired
    JqdbSettingProecssMapper   jqdbSettingsProcessMapper;
    @Autowired
    JqdbSettingsInputMapper  jqdbSettingsInputMapper;
    @Autowired
    JqdbMachinepackingProcessMapper jqdbMachinepackingProcessMapper;
     @Autowired
    CgMaterialMapper cgMaterialMapper;
    @Autowired
    JqdbMaterialMapper  jqdbMaterialMapper;
    @Override
    public Boolean updateJqdbMachinepacking(JqdbMachinepacking jqdbMachinepacking, Token token) {
        //待办修改
        if (jqdbMachinepacking.getTaskstate()==0 && jqdbMachinepacking.getTaskfinishstate()==0 &&
                "0".equals(jqdbMachinepacking.getIstaskstart()) &&  "0".equals(jqdbMachinepacking.getIstaskbegin()) &&
                "0".equals(jqdbMachinepacking.getIstaskend())  && "0".equals(jqdbMachinepacking.getIsreviewtask())
        ){
            jqdbMachinepacking.setTaskreleaseperson(token.getUser().getId().toString());
            jqdbMachinepacking.setTaskreleaseaddtime(new Date());

            return  super.updateById(jqdbMachinepacking);
        }
        //接收任务
        if (jqdbMachinepacking.getTaskstate()==0 && jqdbMachinepacking.getTaskfinishstate()==0 &&
                "1".equals(jqdbMachinepacking.getIstaskstart()) &&  "0".equals(jqdbMachinepacking.getIstaskbegin()) &&
                "0".equals(jqdbMachinepacking.getIstaskend())  && "0".equals(jqdbMachinepacking.getIsreviewtask())
        ){
            jqdbMachinepacking.setIstaskstart("1");
            jqdbMachinepacking.setTaskstartaddtime(new Date());
            jqdbMachinepacking.setTaskstate(1);
            return  super.updateById(jqdbMachinepacking);
        }
        //开始任务
        if (jqdbMachinepacking.getTaskstate()==1 && jqdbMachinepacking.getTaskfinishstate()==0 &&
                "1".equals(jqdbMachinepacking.getIstaskstart()) &&  "1".equals(jqdbMachinepacking.getIstaskbegin()) &&
                "0".equals(jqdbMachinepacking.getIstaskend())  && "0".equals(jqdbMachinepacking.getIsreviewtask())
        ){
            jqdbMachinepacking.setIstaskbegin("1");
            jqdbMachinepacking.setTaskbeginaddtime(new Date());
            return super.updateById(jqdbMachinepacking);
        }
        //结束任务
        if (jqdbMachinepacking.getTaskstate()==1 && jqdbMachinepacking.getTaskfinishstate()==0 &&
                "1".equals(jqdbMachinepacking.getIstaskstart()) &&  "1".equals(jqdbMachinepacking.getIstaskbegin()) &&
                "1".equals(jqdbMachinepacking.getIstaskend())  && "0".equals(jqdbMachinepacking.getIsreviewtask())
        ){
            jqdbMachinepacking.setIstaskend("1");
            jqdbMachinepacking.setTaskfinishstate(1);
            jqdbMachinepacking.setTaskendaddtime(new Date());
            //生成成品库记录
            JqcpMachineproduct jqcpMachineproduct=new JqcpMachineproduct();
            jqcpMachineproduct.setIstaskstart("0");
            jqcpMachineproduct.setIstaskbegin("0");
            jqcpMachineproduct.setIstaskend("0");
            jqcpMachineproduct.setIsreviewtask("0");
            jqcpMachineproduct.setTaskstate(0);
            jqcpMachineproduct.setTaskfinishstate(0);
            jqcpMachineproduct.setResourcename(jqdbMachinepacking.getResourcename());
            jqcpMachineproduct.setTaskcode(jqdbMachinepacking.getTaskcode());
            jqcpMachineproduct.setTypenums(jqdbMachinepacking.getTypenums());
            jqcpMachineproduct.setBatchnums(jqdbMachinepacking.getBatchnums());
            jqcpMachineproductMapper.insert(jqcpMachineproduct);
            return super.updateById(jqdbMachinepacking);
        }
        //复核任务
        if (jqdbMachinepacking.getTaskstate()==1 && jqdbMachinepacking.getTaskfinishstate()==1 &&
                "1".equals(jqdbMachinepacking.getIstaskstart()) &&  "1".equals(jqdbMachinepacking.getIstaskbegin()) &&
                "1".equals(jqdbMachinepacking.getIstaskend())  && "1".equals(jqdbMachinepacking.getIsreviewtask())
        ){
            jqdbMachinepacking.setIsreviewtask("1");
            jqdbMachinepacking.setReviewaddtime(new Date());
            jqdbMachinepacking.setReviewperson(token.getUser().getId().toString());
            return super.updateById(jqdbMachinepacking);
        }

        return null;
    }

    @Override
    public Page<JqdbMachinepacking> selectJqdbMachinepacking(Page<JqdbMachinepacking> page ,QueryJqdbMachinepacking queryJqdbMachinepacking) {
        Wrapper<JqdbMachinepacking> wrapper =new EntityWrapper<>();
        wrapper.eq("TASKSTATE",queryJqdbMachinepacking.getTaskState());
        wrapper.eq("TASKFINISHSTATE",queryJqdbMachinepacking.getTaskFinishState());
        wrapper.eq("isReviewTask",queryJqdbMachinepacking.getIsReviewTask());
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqdbMachinepacking.getCode(),null)){
            wrapper.like("TASKCODE",queryJqdbMachinepacking.getCode());
        }
        if (AdscmBaseUtils.getInstance().isNotEmpty(queryJqdbMachinepacking.getName(),null)){
            wrapper.eq("RESOURCENAME",queryJqdbMachinepacking.getName());
        }

       page = super.selectPage(page,wrapper);
        List<JqdbMachinepacking> jqdbMachinepackings = page.getRecords();
        for (JqdbMachinepacking jqdbMachinepacking : jqdbMachinepackings) {
            if ( jqdbMachinepacking.getTaskreleaseperson()!=null && !"".equals( jqdbMachinepacking.getTaskreleaseperson())){
                JyUser jyUser = jyUserMapper.selectById(jqdbMachinepacking.getTaskreleaseperson());
                jqdbMachinepacking.setTaskreleasepersonName(jyUser.getName());
            }
            if ( jqdbMachinepacking.getTaskperformperson()!=null && !"".equals( jqdbMachinepacking.getTaskperformperson())){
                JyUser jyUser = jyUserMapper.selectById(jqdbMachinepacking.getTaskperformperson());
                jqdbMachinepacking.setTaskperformpersonName(jyUser.getName());
            }

            if ( jqdbMachinepacking.getReviewperson()!=null && !"".equals( jqdbMachinepacking.getReviewperson())){
                JyUser jyUser = jyUserMapper.selectById(jqdbMachinepacking.getReviewperson());
                jqdbMachinepacking.setReviewpersonName(jyUser.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jqdbMachinepacking.getResourcename(),null)){
                JyCode jyCode = jyCodeMapper.selectById(jqdbMachinepacking.getResourcename());
                jqdbMachinepacking.setResourcenameName(jyCode.getName());
            }


        }
        page.setTotal(selectCount(wrapper));
        return page;
    }

    @Override
    public List<JyUser> selectTaskPerformPerson() {
        List<JyUser> jyUsers = jyUserMapper.selectTaskPerformPerson("98");
        return jyUsers;
    }
@Transactional(rollbackFor = Exception.class)
    @Override
    public boolean insertJqdbMachinepacking(JqdbMachinepacking jqdbMachinepacking) throws AdscmSystemException {
        //
        String taskCode= jqdbMachinepacking.getResourcename();
        //根据机器编码查询这个是否有这个总装
        Wrapper<JqzzMachine> jqzzMachineWrapper =new EntityWrapper<>();
        jqzzMachineWrapper.eq("TASKCODE",taskCode);
        List<JqzzMachine> jqzzMachines = jqzzMachineMapper.selectList(jqzzMachineWrapper);
        if (!(jqzzMachines!=null && jqzzMachines.size()>0)){
            throw  new AdscmSystemException("不存在这样的机器编码，请核对");
        }
        Wrapper<JqdbMachinepacking>  jqdbMachinepackingWrapper=new EntityWrapper<>();
        jqdbMachinepackingWrapper.eq("TASKCODE",taskCode);
        List<JqdbMachinepacking> jqdbMachinepackings = super.selectList(jqdbMachinepackingWrapper);
        if (jqdbMachinepackings!=null && jqdbMachinepackings.size()>0){
            throw  new AdscmSystemException("存在机器编码的出厂检测记录");
        }
        //根据机型生成对应的检测记录
        JqzzMachine jqzzMachine = jqzzMachines.get(0);
        JqzzSetting jqzzSetting = jqzzSettingMapper.selectById(jqzzMachine.getResourcename());


        Wrapper<JqdbSettings> jqdbSettingsWrapper =new EntityWrapper<>();
        jqdbSettingsWrapper.eq("type",jqzzSetting.getType());
        jqdbSettingsWrapper.eq("isEnable",1);

        List<JqdbSettings> jqdbSettingss = jqdbSettingsMapper.selectList(jqdbSettingsWrapper);
        for (JqdbSettings jqdbSettings : jqdbSettingss) {
            jqdbMachinepacking.setBatchnums(jqzzMachine.getBatchnums());
            jqdbMachinepacking.setTypenums(jqzzMachine.getTypenums());
            jqdbMachinepacking.setTaskcode(taskCode);
            jqdbMachinepacking.setResourcename(jqdbSettings.getId().toString());
            jqdbMachinepacking.setTaskreleaseaddtime(new Date());
            jqdbMachinepacking.setTaskfinishstate(0);
            jqdbMachinepacking.setTaskstate(0);
            jqdbMachinepacking.setIstaskstart("0");
            jqdbMachinepacking.setIstaskbegin("0");
            jqdbMachinepacking.setIstaskend("0");
            jqdbMachinepacking.setIsreviewtask("0");
            jqdbMachinepacking.setReviewresult("-1");
            super.insert(jqdbMachinepacking);
            //查询这个记录对应的过程记录设置
            Wrapper<JqdbSettingProecss> jqdbSettingsProcessWrapper=new EntityWrapper<>();
            jqdbSettingsProcessWrapper.eq("pid" ,jqdbSettings.getId());
            jqdbSettingsProcessWrapper.eq("isEnable",1);
            List<JqdbSettingProecss> jqdbSettingsProcesses = jqdbSettingsProcessMapper.selectList(jqdbSettingsProcessWrapper);
            for (JqdbSettingProecss jqdbSettingsProcess : jqdbSettingsProcesses) {

                JqdbMachinepackingProcess  jqdbMachinepackingProecss =new JqdbMachinepackingProcess ();
                jqdbMachinepackingProecss.setPid(jqdbMachinepacking.getTid());
                jqdbMachinepackingProecss.setName(jqdbSettingsProcess.getName());
                jqdbMachinepackingProecss.setCid(jqdbSettingsProcess.getId());
                jqdbMachinepackingProecss.setReference(jqdbSettingsProcess.getReference());
                jqdbMachinepackingProecss.setRemark(jqdbSettingsProcess.getRemark());
                jqdbMachinepackingProecss.setRank(jqdbSettingsProcess.getRank());
                jqdbMachinepackingProecss.setType(jqdbSettingsProcess.getType());
                jqdbMachinepackingProecss.setUnit(jqdbSettingsProcess.getUnit());
                jqdbMachinepackingProcessMapper.insert(jqdbMachinepackingProecss);
            }
            //查询对应的输入物料
            Wrapper<JqdbSettingsInput> jqdbSettingsInputWrapper =new EntityWrapper<>();
            jqdbSettingsInputWrapper.eq("pid" ,jqdbSettings.getId());
            List<JqdbSettingsInput> jqdbSettingsInputs = jqdbSettingsInputMapper.selectList(jqdbSettingsInputWrapper);
            for (JqdbSettingsInput jqdbSettingsInput : jqdbSettingsInputs) {
                JqdbMaterial jqdbMaterial =new JqdbMaterial();
                CgMaterial cgMaterial = cgMaterialMapper.selectById(jqdbSettingsInput.getCid());
                jqdbMaterial.setTid(jqdbMachinepacking.getTid());
                jqdbMaterial.setIsAssembly(0);
                jqdbMaterial.setName(jqdbSettingsInput.getCid().toString());

                if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getCode(),null)){
                    jqdbMaterial.setCode(cgMaterial.getCode());
                }
                if (AdscmBaseUtils.getInstance().isNotEmpty(cgMaterial.getType(),null)){
                    jqdbMaterial.setType(cgMaterial.getType());
                }
                jqdbMaterial.setNum(jqdbSettingsInput.getNum());
                jqdbMaterialMapper.insert(jqdbMaterial);
            }

        }
        return true;
    }
}
